import axios from 'axios';
import utils from '@/utils/util';

// 通用公用方法
const req = (method, url, params) => {
    return axios({
        method: method,
        url:  utils.gettootpath() + url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: params,
        traditional: true,
        transformRequest: [
            function (data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                console.log(ret);
                return ret
            }
        ]
    }).then(res => res.data);
};
const reqjson = (method, url, params) => {
    return axios({
        method: method,
        url:  utils.gettootpath() + url,
        headers: {
            'Content-Type': 'application/json',
        },
        data: params,
        traditional: true,
        transformRequest: [
            // function (data) {
            //     let ret = ''
            //     for (let it in data) {
            //         ret +=
            //             encodeURIComponent(it) +
            //             '=' +
            //             encodeURIComponent(data[it]) +
            //             '&'
            //     }
            //     console.log(ret);
            //     return ret
            // }
        ]
    }).then(res => res.data);
};
const reqfiles = ( url, params) => {
   let u= utils.gettootpath()+url; 
    return axios.post( u,params, {
        'Content-Type': 'multipart/form-data'
    }).then(res => res.data);
};
export {
    req,reqfiles,reqjson
}